package com.reservationserver.mapper.user;

import com.reservationpojo.entity.Badminton;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserBadmintonMapper {
    //获取选中时段的剩余位置数量
    @Select("select count(position) from badminton where reservation_start_time=#{starttime} " +
            "and status='未到场'")
    Integer freePositionNum(LocalDateTime starttime);
    //查找已经被选过的位置
    @Select("select position from badminton where reservation_start_time=#{starttime} " +
            "and reservation_end_time=#{endtime} and (status='未到场' or status='已签到')")
    List<String> positionSelectByTime(LocalDateTime starttime,LocalDateTime endtime);
    //预约操作
    @Insert("insert into badminton(reservation_start_time,reservation_end_time, position, reserved_byid,status,create_time) " +
            "values (#{reservationStartTime},#{reservationEndTime},#{position},#{reservedByid},#{status},#{createTime})")
    void reservePosition(Badminton badminton);
    //查找是否被拉黑
    @Select("select black from user where student_id=#{reservationByid}")
    boolean isBlack(String reservationByid);
    //获取该用户的全部预约记录的日期
    @Select("select reservation_start_time from badminton where reserved_byid=#{reservedByid}")
    List<LocalDateTime> reservationStartTimeList(String reservedByid);
    //标记表中查找是否允许重复预约
    @Select("select badminton from isrepeate where flag='1';")
    boolean isrepeeate();
    //清除超时预约
    @Update("update badminton set status = '已迟到' where reservation_start_time=#{time};")
    void clean(LocalDateTime time);
    //提前一小时通知有预约的用户
    @Select("select reserved_byid from badminton where reservation_start_time=#{time} and status='未到场'")
    List<String> idSelectByTime(LocalDateTime time);
    //用户查询自己的预约记录
    @Select("select position, reserved_byid, reservation_start_time, reservation_end_time,status,create_time,status from badminton " +
            "where reserved_byid=#{uid} limit #{page},10")
    List<Badminton> checkReservation(String uid,Integer page);
    //删除预约操作，将该预约的状态改为“已取消”
    @Update("update badminton set status=#{status} where reservation_start_time=#{reservationStartTime} and " +
            "reservation_end_time=#{reservationEndTime} and position=#{position} and reserved_byid=#{reservedByid} and create_time=#{createTime}")
    void deleteReservation(Badminton badminton);
    @Delete("delete from badminton where create_time like concat(#{date},'%')")
    void cleanOverDate(String date);
    //通过时间获取用户id
    @Select("select reserved_byid from badminton where reservation_start_time=#{time}")
    List<String> getRecord(LocalDateTime time);
    //对指定用户违规次数进行修改
    @Update("update user set violation_count=#{count} where student_id=#{badmintonId}")
    void recordBeforeClean(String badmintonId,Integer count);
    //通过id获取当前用户违规次数
    @Select("select violation_count from user where student_id=#{badmintonId}")
    Integer getUserCount(String badmintonId);
    //查询被关闭的场地
    @Select("select court from shutdowncourt where court=#{court}")
    String checkShutCourt(String court);
    //获取被关闭的场地
    @Select("select court from shutdowncourt where court like concat(#{court},'%')")
    List<String> getCourt(String court);
    @Select("select reserved_byid from badminton where reservation_start_time=#{reservationStartTime} " +
            "and reservation_end_time=#{reservationEndTime} and position=#{position}")
    String isFree(Badminton badminton);
}
